package worldinruin.gui.imp.gameview;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import worldinruin.GameDataHandler;
import worldinruin.IGameDataHandler;
import worldinruin.controllers.ControlFactory;
import worldinruin.controllers.ControlFactory.Controllers;
import worldinruin.core.ISoldier;
import worldinruin.resources.Components;
import worldinruin.resources.Keys;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TrainSoldiersPanel extends javax.swing.JPanel {

	private IGameDataHandler dataHandler = GameDataHandler.getInstance();	
	private ArrayList<ISoldier> soldiers = dataHandler.getSoldierTypes();
	private JButton trainSoldiersButton;
	private ArrayList<SoldierPanel> soldierPanels = new ArrayList<SoldierPanel>();
	private JPanel soldierPanel;
	private Action listener;
	
	private Color brown = new Color(72,65,49);
	private Color white = new Color(243,249,213);

	/**
	 * Creates a train soldiers panel.
	 */
	public TrainSoldiersPanel() {
		super();
		initGUIManually();
		initListener();

	}
	
	/**
	 * Initiates listeners to components.
	 */
	private void initListener() {
		listener = ControlFactory.getControl(Controllers.GAMEBOARD_CONTROLLER);
		trainSoldiersButton.addActionListener(listener);
		listener.putValue(Components.trainSoldiersButton, trainSoldiersButton);
		listener.putValue(Keys.UNITAMOUNT.toString(), this);
	}
	
	/**
	 * Gets all unit amounts the user wants to train.
	 * @return Values of all units.
	 */
	public int[] getValues() {
		int[] values = new int[4];
		
		for (int c = 0; c < soldierPanels.size(); c++) {
			values[c] =  soldierPanels.get(c).getAmount();
			
		}
	
		return values;
	
	}
	
	/**
	 * Initiates the GUI.
	 */
	private void initGUIManually(){
		
		GroupLayout thisLayout = new GroupLayout((JComponent)this);
		{
			soldierPanel = new JPanel();
			GridLayout soldierPanelLayout = new GridLayout(1, 1);
			soldierPanelLayout.setColumns(1);
			soldierPanelLayout.setHgap(5);
			soldierPanelLayout.setVgap(5);
			soldierPanel.setLayout(soldierPanelLayout);
		}
		{
			trainSoldiersButton = new JButton();
			trainSoldiersButton.setText("Train units");
		}
		soldierPanel.setLayout(new GridLayout(soldiers.size(), 1));

		for (ISoldier soldier : soldiers) {			
			SoldierPanel soldierPanel = new SoldierPanel(soldier);
			soldierPanels.add(soldierPanel);
			this.soldierPanel.add(soldierPanel);			
		}
		
		this.setLayout(thisLayout);
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addComponent(soldierPanel, 0, 141, Short.MAX_VALUE)
				.addGap(15)
				.addComponent(trainSoldiersButton, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
				.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createParallelGroup()
				.addComponent(soldierPanel, GroupLayout.Alignment.LEADING, 0, 167, Short.MAX_VALUE)
				.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				    .addGap(29)
				    .addComponent(trainSoldiersButton, 0, 73, Short.MAX_VALUE)
				    .addContainerGap(65, 65)));

	}
}
